/*
 *
 *  * Copyright:  2018 smarabbit studio.
 *  *
 *  *  Licensed under the Confluent Community License; you may not use this file
 *  *  except in compliance with the License.  You may obtain a copy of the License at
 *  *
 *  *  http://www.confluent.io/confluent-community-license
 *  *
 *  *  Unless required by applicable law or agreed to in writing, software
 *  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 *  *  WARRANTIES OF ANY KIND, either express or implied.  See the License for the
 *  *  specific language governing permissions and limitations under the License.
 *  *
 *  *  @作   者： 黄开晖 (117227773@qq.com)
 *  *  @日   期:    ${DATE}
 *
 */

package com.massyframework.beanskin.util.sorting.graph;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.function.Function;

/**
 * {@link SortResult}, 排序后的结果
 * @param <T>
 */
public class SortResult<T> {

    private List<T> sorted;
    private List<T> unsort;

    /**
     * 创建一个新实例
     */
    public SortResult(){
        this.sorted = new ArrayList<>();
        this.unsort = Collections.emptyList();
    }

    /**
     * 添加已经排序完成的对象集合
     * @param sorted {@link List}
     */
    public void addSorted(List<T> sorted){
        this.sorted.addAll(sorted);
    }

    /**
     * 设置不能排出结果的对象集
     * @param unsort {@link List}
     */
    public void setUnsort(List<T> unsort){
        this.unsort = unsort;
    }

    /**
     * 判断是否全部都能进行排序
     * @return {@link boolean},返回{@code true}表示是，否则返回{@code false}
     */
    public boolean isSortAll(){
        return this.unsort.isEmpty();
    }

    /**
     * 已排序对象
     * @return {@link List}
     */
    public List<T> getSorted(){
        return this.sorted;
    }

    /**
     * 未排序对象
     * @return {@link List}
     */
    public List<T> getUnsort(){
        return this.unsort;
    }

    /**
     * 所有排序对象总数
     * @return {@link int}
     */
    public int size(){
        return this.sorted.size() + this.unsort.size();
    }


    /**
     * 对SortResult进行转换为另一种SortResult
     * @param source {@link SortResult}, 源
     * @param func {@link Function},转换函数
     * @param <T>
     * @param <R>
     * @return {@link SortResult}
     */
    public static <T, R> SortResult<R> toConvert(SortResult<T> source, Function<T, R> func){
        List<R> sorted = source.getSorted().stream()
                .map( value -> func.apply(value)).collect(Collectors.toList());
        List<R> unsorted = source.getUnsort().isEmpty() ? Collections.emptyList() :
                source.getUnsort().stream().map(value -> func.apply(value)).collect(Collectors.toList());
        SortResult<R> result = new SortResult<R>();
        result.addSorted(sorted);
        result.setUnsort(unsorted);

        return result;
    }
}
